/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MYAPPLICATION_CORDOVAHTTPPLUGIN_H
#define MYAPPLICATION_CORDOVAHTTPPLUGIN_H

#include "CordovaPlugin.h"
#include "cJSON.h"
#include "Thread.h"

class CordovaHttpPlugin:public CordovaPlugin {
    static const int LOG_PRINT_DOMAIN = 0xff00;
    static const string LINE_START;
    static const string LINE_END;
    static const string BOUNDARY;
    class HttpRequest : public CThread{
        int m_nTempBufSize{0};
        string m_strMethod;
        CordovaHttpPlugin* m_cordovaHttp{nullptr};
        cJSON* m_pArgs{nullptr};
        CallbackContext m_cbc;
        int m_nReqId{-1};
        std::atomic<bool> m_isAbort{false};
    public:
        HttpRequest(){};
        ~HttpRequest(){}
        void Execute(void* arg);
        void SetCordovaHttpPlugin(const string& strMethod, CordovaHttpPlugin* pCordovaHttp, cJSON* pJson, CallbackContext cbc) {
            m_strMethod = strMethod;
            m_cordovaHttp = pCordovaHttp;
            m_pArgs = pJson;
            m_cbc = cbc;
        }
        void* InitThreadData();
	    void DeInitThreadData(void* pArg);
        void setReqId(const int nReqId) {m_nReqId = nReqId;}
        int getReqId() const { return m_nReqId;}
        void setAbort(){m_isAbort.store(true);};
        std::atomic<bool>& getAbort() {return m_isAbort;}
    };
    int m_nTLSVersion;
    std::mutex m_mutex;
    string m_strCAFilePathName;
    string m_strServerTrustMode{"nocheck"};
    string m_strClientCertPathName;
    string m_strClientKeyPathName;
    void initialize();
    void setHttpRequestThread(const HttpRequest* pRequest);
    bool setHttpRequestThreadAbort(const int nReqId);
    vector<HttpRequest*> m_vecHttpRequestThread;
    bool isNetworkAvailable();
    string getData(const string strSerializer, cJSON* pJson);
    bool executeHttpRequestWithData(const string& strMethod, cJSON* pJson, CallbackContext cbc, void* pTmp, const int nBufSize, HttpRequest* pRequest);
    bool executeHttpRequestWithoutData(const string& strMethod, cJSON* pJson, CallbackContext cbc, void* pTmp, const int nBufSize, HttpRequest* pRequest);
    bool downloadFile(cJSON* pJson, CallbackContext cbc, void* pTmp, const int nBufSize, HttpRequest* pRequest);
    bool uploadFiles(cJSON* pJson, CallbackContext cbc, void* pTmp, const int nBufSize, HttpRequest* pRequest);
public:
    CordovaHttpPlugin();
    ~CordovaHttpPlugin();
    bool execute(const string&  action, cJSON* args, CallbackContext cbc) override;
};

#endif //MYAPPLICATION_CORDOVAHTTPPLUGIN_H
